const puppeteer = require("puppeteer-core");
let fs = require("fs");
let path = require("path");

async function pc() {
    const browser = await puppeteer.launch({
        headless: false, //有浏览器界面启动
        slowMo: 10, //放慢浏览器执行速度，方便测试观察
        ignoreHTTPErrors: true,
        defaultViewport: { width: 1400, height: 700 },
        args: [
            //启动 Chrome 的参数，详见上文中的介绍
            //   "–no-sandbox",
            // '--start-fullscreen'
        ],
    });
    const page = await browser.newPage();
    await page.goto("http://oa.hymson.com:8012/seeyon/main.do");
    //   console.log(page, "page---------------");
    let user = await page.$("#login_username");
    let pass = await page.$("#login_password1");
    let loginbtn = await page.$("#login_button");
    await user.type("18662",);
    await pass.type("Aa123456",);


    await loginbtn.click();
    await page.waitForNavigation();
    await p(2000);
    let tables = await page.$(".chessboardtable");
    // console.log(tables, 'taaaa')
    await page.evaluate((el) => {
        console.log(el, "el");
        // let ta = el.querySelector('.chessboardtable')
        // console.log(ta, 'tttttt')
        let a = el?.querySelector("a");
        a?.click();
    }, tables);
    const newPagePromise = new Promise((res) =>
        browser.on("targetcreated", async (target) => {
            const newTarget = await target.page();
            // const content = await newTarget.content()
            res(newTarget);
        })
    );
    //点击按钮后，等待新tab对象
    let newPage = await newPagePromise;
    await p(2000);

    let iframe = await newPage.$('#zwIframe')
    newPage.evaluate(el => {
        console.log(el, 'dedede')
        let body = el.contentWindow.document
        let time = body.querySelector('#field0010').value = '2022-02-02'

        body.querySelector('#field0030_txt').nextSibling.click()
        // console.log(d, 'ddd')
        // [0]
        // p(100)
        let sdiv = body.querySelector('.menu')
        sdiv.children[18].click()
        console.log(sdiv, 'diiiii', sdiv?.children[18])
        //.children()[18].click()  $('#field0030_txt')[0].nextSibling.click()
        //   let start = await newPage.$("#field0030_txt");
        //   let end = await newPage.$("#field0031_txt");
        //   let jieduan = await newPage.$("#field0028_txt");
        //   let gongsi = await newPage.$("#field0014");
        //   let didian = await newPage.$("#field0012_txt");
        //   let jingl = await newPage.$("#field0014");

    }, iframe)

    let iframess = page.mainFrame().childFrames()
    //.childFrames()
    // console.log(iframess, 'iframe')
    // let framess = page.frames()
    // .find(item => {
    //     console.log(item.name(), 'item.name()')
    //     return item.name() == 'playSoundHelper'
    // })
    // p(2000)
    iframess.forEach(async item => {
        let head = await item.$('#mainbodyHtmlDiv_0')
        console.log(head, 'frameframeframe')
    })


    // ///seeyon/content/content.do?method=index&isFullPage=true&formpage=newcol&isNew=false&moduleId=7660406401775780470&moduleType=1&rightId=-374647880168750247.4827016278860449013&isSubFlow=false&contentType=20&originalNeedClone=true&transOfficeId=&fromIframeName=newCollaboration&subState=&viewState=1&rnd=0.013017718573005821
    // const text = await frame.$eval('#field0010', element => element.textContent);
    // console.log(text, 'texttexttext')
    // let
    // for (const frame of iframe) {
    //     //根据 url 找到登陆页面对应的 iframe
    //     console.log(frame._id, 'frame.url()')
    //     if (frame._id == '7916C7E5AF53BFCD4A2BBE60B1A69924') {
    //         console.log('zhaodaole')

    // await frame?.type('#field0010', '2022-01-01');
    // await iframe.type('.dlpwd', '123456');
    //         // await Promise.all([
    //         //     frame.click('#dologin'),
    //         //     page.waitForNavigation()
    //         // ]);
    //         break;
    //     }
    // }
    // console.log('nnnn', newPage)
    /*
    let copy = await newPage.$("#copyToForm");
    //   await copy.click();
    //   await newPage.evaluate((el) => {
    //     el.click(this);
    //   }, copy);
    let tt = await newPage.$(".xdLayout");
    let time = await newPage.$("#field0010");
    let div = await newPage.$("#mainbodyHtmlDiv_0");
    //   let start = await newPage.$("#field0030_txt");
    //   let end = await newPage.$("#field0031_txt");
    //   let jieduan = await newPage.$("#field0028_txt");
    //   let gongsi = await newPage.$("#field0014");
    //   let didian = await newPage.$("#field0012_txt");
    //   let jingl = await newPage.$("#field0014");
    console.log(time, "timetimetimetime", copy, tt);
    console.log(div, "eeeeee");
    await time?.type("2022-01-01");
    //   await start.type("08:30");
    //   await end.type("18:00");
    //   await jieduan.type("研发");
    //   await gongsi.type("8");
    //   await didian.type("深圳海目星");
    */
}

pc();

function p(t = 1000) {
    return new Promise((res) => {
        setTimeout(() => {
            res();
        }, t);
    });
}
